package com.yonyou.component;

import com.intellij.notification.NotificationType;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.NlsSafe;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.ui.awt.RelativePoint;
import git4idea.commands.GitLineHandlerListener;
import git4idea.repo.GitRepository;

import javax.swing.*;

/**
 * <p>
 *
 * @author fengjiening
 * @since 2024-01-27
 */
public class CtmGitLineHandlerListener implements GitLineHandlerListener {
    private  StatusBar statusBar;
    private GitRepository repository;
    private ProgressIndicator progressIndicator;
    public CtmGitLineHandlerListener (ProgressIndicator progressIndicator,GitRepository repository,StatusBar statusBar){
        super();
        this.statusBar= statusBar;
        this.repository = repository;
        this.progressIndicator = progressIndicator;
    }
    @Override
    public void onLineAvailable(@NlsSafe String line, Key outputType) {
        JBPopupFactory instance = JBPopupFactory.getInstance();
        if(line.contains("Switched")){
            instance.createHtmlTextBalloonBuilder(line, MessageType.INFO, null)
                    .setFadeoutTime(3500)
                    .createBalloon()
                    .show(RelativePoint.getCenterOf(statusBar.getComponent()), Balloon.Position.atRight);
            repository.update();
        }else{
            com.yonyou.ctm.ctmplugin.utils.NotificationUtil.showNotification(NotificationType.INFORMATION,line);
            // 开启进度条
            progressIndicator.setIndeterminate(true);
            // 设置进度条为百分百
            progressIndicator.setFraction(1.0);
           // progressIndicator.setText(line);
        }
}
}
